<?
function drizzo_plugins_available() {
	$plugins_directory = root_dir . "/drizzo-addons/plugins/";
	if($folder = opendir($plugins_directory)) {
		while(false !== ($file = readdir($folder))) {
			$plugins_list[] = $file;
		}
		closedir($folder);
	}
	if($plugins_list) {
		foreach($plugins_list as $plugin) {
			if($plugin != '' && $plugin != null && is_file($plugins_directory . $plugin)) {
				$plugins[] = $plugin;
			} elseif($plugin != '' && $plugin != null && is_dir($plugins_directory . $plugin) && file_exists($plugins_directory . $plugin . "/$plugin.php")) {
				$plugins[] = $plugin . "/" . $plugin . ".php";
			}
		}
	}
	if(count($plugins) > 0) {
		return $plugins;
	} else {
		return false;
	}
}

function drizzo_plugin_info($plugin) {
	$plugins_directory = root_dir . "/drizzo-addons/plugins/";
	$plugin_file = $plugins_directory . $plugin;
	$plugin_meta = @file_get_contents($plugin_file, 'r');
	if($plugin_meta) {
		$plugin_meta = substr($plugin_meta, strpos($plugin_meta, "/*") + 2, (strpos($plugin_meta, "*/") - strpos($plugin_meta, "/*") - 2));
		$plugin_meta = explode("\n",$plugin_meta);
		if($plugin_meta) {
			foreach($plugin_meta as $meta) {
				$meta = explode(": ", $meta);
				$plugin_info[trim(strtolower($meta[0]))] = trim($meta[1]);
			}
		} else {
			$plugin_info['author'] = 'unknown';
			$plugin_info['url'] = 'unknown';
			$plugin_info['name'] = $plugin;
			$plugin_info['version'] = 'unknown';
		}
		return $plugin_info;
	} else {
		return false;
	}
}

function drizzo_plugin_enabled($plugin) {
	global $drizzo_options;
	drizzo_db_connect();
	$plugin = drizzo_db_wrap($plugin);
	$plugin_lookup = @mysql_query("SELECT `value` FROM `$drizzo_options` WHERE `name`='$plugin'");
	if(!mysql_errno()) {
		while($row = @mysql_fetch_assoc($plugin_lookup)) {
			if($row['value'] == 'enabled') {
				return true;
			} else {
				return false;
			}
		}
	} else {
		return false;
	}
}

function drizzo_plugins_table() {
	$plugins = drizzo_plugins_available();
	if($plugins) {
		$table = "<table>\n";
		$table .= "\t<tr id=\"first\">\n";
		$table .= "\t\t<th>Plugin Name</th><th class=\"narrow center\">Version</th><th>Description</th><th>Author</th><th class=\"center narrow\">Enabled</th>";
		if(drizzo_user_is_allowed('plugins')) {
			$table .= "<th class=\"narrow center\">Action</th>\n";
		} else {
			$table .= "\n";
		}
		$table .= "\t</tr>\n";
		foreach($plugins as $plugin) {
			unset($plugin_meta,$enabled);
			$plugin_meta = drizzo_plugin_info($plugin);
			@extract($plugin_meta);
			$enabled = drizzo_plugin_enabled($plugin);
			if ($alt_count%2) { 
				$table .= "\t<tr class=\"alt\" id=\"$plugin\">\n";
			} else {
				$table .= "\t<tr id=\"$plugin\">\n";
			}
			$table .= "\t\t<td>$name";
			if(drizzo_plugin_options($plugin) && $enabled) {
				$table .= " <a href=\"plugins.php?plugin=$plugin\" title=\"$name Options\">(Options)</a>";
			}
			$table .= "</td><td class=\"center\">$version</td><td>$description";
			if(drizzo_plugin_help($plugin) && $enabled) {
				$table .= " <a href=\"plugins.php?help=$plugin\" title=\"$name Instructions\">(Instructions)</a>";
			}
			$table .= "</td><td><a href=\"$url\" title=\"$author\">$author</a></td><td class=\"center\">";
			if($enabled) {
				$table .= "Yes";
			} else {
				$table .= "No";
			}
			$table .= "</td>";
			if(drizzo_user_is_allowed('plugins')) {
				if(!$enabled) {
					$table .= "<td class=\"center\"><a href=\"plugins.php?enable=$plugin\" title=\"Enable $name\"><img src=\"" . drizzo_admin_style_folder() . "images/approved.png\" alt=\"Enable\" /></a></td>";
				} else {
					$table .= "<td class=\"center\"><a href=\"plugins.php?disable=$plugin\" title=\"Disable $name\"><img src=\"" . drizzo_admin_style_folder() . "images/delete.png\" alt=\"Disable\" /></a></td>";
				}
			}
			$table .= "\n\t</tr>\n";
			$alt_count++;
		}
		$table .= "</table>\n";
	} else {
		$table = "<br class=\"clear\" /><p>There are no plugins available</p><br class=\"clear\" />";
	}
	return $table;
}

function drizzo_plugins_enabled() {
	$plugins = drizzo_plugins_available();
	if($plugins) {
		foreach($plugins as $plugin) {
			if(drizzo_plugin_enabled($plugin)) {
				$active_plugins[] = $plugin;
			}
		}
		return $active_plugins;
	} else {
		return false;
	}
}

function drizzo_plugin_options($plugin) {
	$plugins_directory = root_dir . "/drizzo-addons/plugins/";
	$plugin_file = $plugins_directory . $plugin;
	$plugin_options = @file_get_contents($plugin_file, 'r');
	if($plugin_options) {
		$options_start = strpos($plugin_options, "/* Options");
		if($options_start == false) {
			$options_start = strpos($plugin_options, "/* OPTIONS");
		}
		$options_stop = strpos($plugin_options, "*/", $options_start + 10) - 2;
		$plugin_options = trim(substr($plugin_options, $options_start + 10, $options_stop - ($options_start + 10)));
		if(strlen($plugin_options) > 0 && $options_start != false) {
			return $plugin_options;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

function drizzo_plugins_with_options() {
	$plugins = drizzo_plugins_enabled();
	if($plugins) {
		foreach($plugins as $plugin) {
			if(drizzo_plugin_options($plugin)) {
				$list[] = $plugin;
			}
		}
		return $list;
	} else {
		return false;
	}
}

function drizzo_plugin_help($plugin) {
	$plugins_directory = root_dir . "/drizzo-addons/plugins/";
	$plugin_file = $plugins_directory . $plugin;
	$plugin_help = @file_get_contents($plugin_file, 'r');
	if($plugin_help) {
		$help_start = strpos($plugin_help, "/* Help");
		if($help_start == false) {
			$help_start = strpos($plugin_help, "/* HELP");
		}
		$help_stop = strpos($plugin_help, "*/", $help_start + 7) - 2;
		$plugin_help = trim(substr($plugin_help, $help_start + 7, $help_stop - ($help_start + 7)));
		if(strlen($plugin_help) > 0 && $help_start != false) {
			return $plugin_help;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

function drizzo_base_plugins() {
	global $drizzo_options;
	
	$options_query = @mysql_query("SELECT `value` FROM `$drizzo_options` WHERE `name`='base_plugins' ORDER BY `value`");
	if(!mysql_errno()) {
		while($row = @mysql_fetch_assoc($options_query)) {
			$func = drizzo_db_unwrap($row['value']);
			if(function_exists($func)) {
				$func();
			}
		}
	} else {
		return false;
	}
}
?>
